package com.app.service.impl;

import com.app.common.AreaListTool;
import com.app.common.ConstantTool;
import com.app.mapper.jiuquan.*;
import com.app.mapper.suzhou.YouthJobInvestigateMapper;
import com.app.model.*;
import com.app.service.CountNumberService;
import com.app.util.CommonUtil;
import com.app.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
//统计
@Service
public class CountNumberServiceImpl implements CountNumberService {
    @Autowired
    private MentorInfoMapper mentorInfoMapper;
    @Autowired
    private IncubatorsMapper incubatorsMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private TrainPeopleMapper trainPeopleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserCountMapper userCountMapper;
    @Autowired
    private PolicyMapper policyMapper;
    @Autowired
    private YouthJobInvestigateMapper youthJobInvestigateMapper;
    @Autowired
    private LoanInfoMapper loanInfoMapper;

    @Autowired
    private ModuleCountMapper moduleCountMapper;

    @Autowired
    private SiteApplyForMapper siteApplyForMapper;

    @Autowired
    private SpecialityManageMapper specialityManageMapper;


    //统计数量
    @Override
    public CountVo getCountAll() {
        //导师数量
        Integer mentorInfoCount = mentorInfoMapper.selectCount(null);
        //场地数量
        Integer incubatorsCount = incubatorsMapper.selectCount(null);
        //创业项目数量
        Integer projectCount = projectMapper.selectCount(null);
        //创业政策数
        Integer policyCount = policyMapper.selectCount(null);
        //访问数
        Integer userCount = userCountMapper.selectCount(null);
        CountVo countVo = new CountVo();
        countVo.setMentorInfoCount(mentorInfoCount);
        countVo.setIncubatorsCount(incubatorsCount);
        countVo.setProjectCount(projectCount);
        countVo.setPolicyCount(policyCount);
        countVo.setUserCount(userCount);
        return countVo;
    }

    //技能培训报名人数统计 7个城市统计
    @Override
    public XyVo getTrainPeopleCount() {
        XyVo trainPeopleCount = new XyVo();
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        List<EducationVo> areaVoList = ConstantTool.AREA_VO_LIST;
        for (EducationVo educationVo : areaVoList) {
            String city = educationVo.getData();
            Integer cityId = educationVo.getId();
            QueryWrapper<TrainPeopleModel> qw = new QueryWrapper<>();
            qw.eq("city", cityId);
            Integer cityCount = trainPeopleMapper.selectCount(qw);
            nameList.add(city);
            valueList.add(cityCount);
        }
        trainPeopleCount.setXData(nameList);
        trainPeopleCount.setYData(valueList);
        return trainPeopleCount;
    }

    //统计最近15天访问人数
    @Override
    public XyVo getNear15DayCount() {
        XyVo near15dayCount = new XyVo();
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        List<String> dayList = CommonUtil.getDayList(15);
        for (String date : dayList) {
            String startTime = date + " 00:00:00";
            String endTime = date + " 23:59:59";
            QueryWrapper<UserCountModel> qw = new QueryWrapper<>();
            qw.ge("create_time", startTime);
            qw.le("create_time", endTime);
            Integer count = userCountMapper.selectCount(qw);
            String monthDay = date.substring(5);
            nameList.add(monthDay);
            valueList.add(count);
        }
        near15dayCount.setXData(nameList);
        near15dayCount.setYData(valueList);
        return near15dayCount;
    }

    //统计各城市16-24问卷调查
    @Override
    public XyVo getInvestigateCount() {
        XyVo investigateCount = new XyVo();
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        List<EducationVo> areaVoList = ConstantTool.AREA_VO_LIST;
        for (EducationVo educationVo : areaVoList) {
            String city = educationVo.getData();
            QueryWrapper<YouthJobInvestigateModel> qw = new QueryWrapper<>();
            qw.eq("area", city);
            qw.ge("age", 16);
            qw.le("age", 24);
            Integer cityCount = youthJobInvestigateMapper.selectCount(qw);
            nameList.add(city);
            valueList.add(cityCount);
        }
        investigateCount.setXData(nameList);
        investigateCount.setYData(valueList);
        return investigateCount;
    }

    //问卷调查情况分析
    @Override
    public List<ValueAndNameVo> getInvestigateAnalysis() {
        List<ValueAndNameVo> investigateAnalysis = new ArrayList<>();
        ValueAndNameVo vo1 = new ValueAndNameVo();
        ValueAndNameVo vo2 = new ValueAndNameVo();
        ValueAndNameVo vo3 = new ValueAndNameVo();
        //16-24就业调查统计
        QueryWrapper<YouthJobInvestigateModel> qw = new QueryWrapper<>();
        qw.ge("age", 16);
        qw.le("age", 24);
        Integer youthCount = youthJobInvestigateMapper.selectCount(qw);
        vo1.setName("16-24Work");
        vo1.setValue(youthCount);
        //城镇0就业调查统计
        vo2.setName("0Work");
        vo2.setValue(0);
        //2024就业统计
        vo3.setName("2024Work");
        vo3.setValue(0);
        investigateAnalysis.add(vo1);
        investigateAnalysis.add(vo2);
        investigateAnalysis.add(vo3);
        return investigateAnalysis;
    }
    //好评中评差评评价统计
    @Override
    public List<ValueAndNameVo> getCommentTypeCount(Integer type) {

        List<ValueAndNameVo> commentTypeCount = new ArrayList<>();
        ValueAndNameVo vo1 = new ValueAndNameVo();
        ValueAndNameVo vo2 = new ValueAndNameVo();
        ValueAndNameVo vo3 = new ValueAndNameVo();

        QueryWrapper<CommentModel> commentTypeQW2 =new QueryWrapper<>();
        commentTypeQW2.eq("type",type);
        commentTypeQW2.eq("comment_type",2);
        Integer commentType2 = commentMapper.selectCount(commentTypeQW2);

        QueryWrapper<CommentModel> commentTypeQW3 =new QueryWrapper<>();
        commentTypeQW3.eq("type",type);
        commentTypeQW3.eq("comment_type",3);
        Integer commentType3 = commentMapper.selectCount(commentTypeQW3);

        QueryWrapper<CommentModel> commentTypeQW4 =new QueryWrapper<>();
        commentTypeQW4.eq("type",type);
        commentTypeQW4.eq("comment_type",4);
        Integer commentType4 = commentMapper.selectCount(commentTypeQW4);

        vo1.setName("好评");
        vo2.setName("中评");
        vo3.setName("差评");

        vo1.setValue(commentType2);
        vo2.setValue(commentType3);
        vo3.setValue(commentType4);

        commentTypeCount.add(vo1);
        commentTypeCount.add(vo2);
        commentTypeCount.add(vo3);
        return commentTypeCount;
    }

    //贷款类型人数分析
    @Override
    public List<ValueAndNameVo> getLoanTypeAnalysis() {
        List<ValueAndNameVo> getLoanTypeAnalysis = new ArrayList<>();
        List<String> titleList = loanInfoMapper.selectName();
        for (String title : titleList) {
            ValueAndNameVo vo =new ValueAndNameVo();
            QueryWrapper<LoanInfoModel> qw =new QueryWrapper<>();
            qw.eq("title",title);
            Integer count = loanInfoMapper.selectCount(qw);
            vo.setName(title);
            vo.setValue(count);
            getLoanTypeAnalysis.add(vo);
        }
        return getLoanTypeAnalysis;
    }

    //我市创业困难原因分析
    @Override
    public List<ValueAndNameVo> getDifficultyAnalysis() {
        List<ValueAndNameVo> difficultyAnalysis =new ArrayList<>();
        List<ModuleCountModel> moduleCountList = moduleCountMapper.selectList(null);
        for (ModuleCountModel moduleCount : moduleCountList) {
            ValueAndNameVo vo =new ValueAndNameVo();
            vo.setName(moduleCount.getName());
            vo.setValue(moduleCount.getCount());
            difficultyAnalysis.add(vo);
        }
        return difficultyAnalysis;
    }

    //创业场地申请次数
    @Override
    public XyVo getSiteApplyForCount() {
        XyVo vo =new XyVo();
        List<String> nameList = incubatorsMapper.selectNameList();
        List<Integer> siteApplyForCount = siteApplyForMapper.getSiteApplyForCount();
        vo.setXData(nameList);
        vo.setYData(siteApplyForCount);
        return vo;
    }
    //培训情况分析专业数量不包括0查出来
//    @Override
//    public XyVo getTrainSituation(Integer type) {
//        XyVo vo =new XyVo();
//        List<String> nameList=new ArrayList<>();
//        List<Integer> countList=new ArrayList<>();
//        QueryWrapper<TrainPeopleModel>qw =new QueryWrapper<>();
//        qw.select("DISTINCT speciality_id,train_type");
//        qw.eq("train_type",type);
//        List<TrainPeopleModel> trainPeopleList = trainPeopleMapper.selectList(qw);
//
//        for (TrainPeopleModel trainPeople : trainPeopleList) {
//            Integer specialityId = trainPeople.getSpecialityId();
//            SpecialityManageModel specialityManage = specialityManageMapper.selectById(specialityId);
//            nameList.add(specialityManage.getName());
//
//            QueryWrapper<TrainPeopleModel> trainPeopleQW=new QueryWrapper<>();
//            trainPeopleQW.eq("train_type",type);
//            trainPeopleQW.eq("speciality_id",specialityId);
//            Integer count = trainPeopleMapper.selectCount(trainPeopleQW);
//            countList.add(count);
//        }
//        vo.setXData(nameList);
//        vo.setYData(countList);
//        return vo;
//    }

    //培训情况分析 专业数量包括0查出来
    @Override
    public XyVo getTrainSituation(Integer type) {
        XyVo vo =new XyVo();
        List<String> nameList=new ArrayList<>();
        List<Integer> countList=new ArrayList<>();
        QueryWrapper<SpecialityManageModel>SpecialityQW =new QueryWrapper<>();
        SpecialityQW.select("id,name");
        List<SpecialityManageModel> specialityNameList = specialityManageMapper.selectList(SpecialityQW);
        for (SpecialityManageModel specialityManage : specialityNameList) {
            nameList.add(specialityManage.getName());
            QueryWrapper<TrainPeopleModel> trainPeopleQW=new QueryWrapper<>();
            trainPeopleQW.eq("train_type",type);
            trainPeopleQW.eq("speciality_id",specialityManage.getId());
            Integer count = trainPeopleMapper.selectCount(trainPeopleQW);
            countList.add(count);
        }
        vo.setXData(nameList);
        vo.setYData(countList);
        return vo;
    }
    //创业导师情况分析
    @Override
    public List<ValueAndNameVo> getMentorSituationAnalysis() {
        List<ValueAndNameVo> voList =new ArrayList<>();
        QueryWrapper<MentorInfoModel> qw =new QueryWrapper<>();
        qw.select("DISTINCT major");
        List<MentorInfoModel> mentorInfoList = mentorInfoMapper.selectList(qw);
        for (MentorInfoModel mentorInfo : mentorInfoList) {
            ValueAndNameVo vo =new ValueAndNameVo();
            String major = mentorInfo.getMajor();
            QueryWrapper<MentorInfoModel>qwCount=new QueryWrapper<>();
            qwCount.eq("major",major);
            Integer count = mentorInfoMapper.selectCount(qwCount);
            vo.setName(major);
            vo.setValue(count);
            voList.add(vo);

        }
        return voList;
    }

    @Override
    public List<AreaVo> getAreaIncubatorsCount() {
        List<AreaVo> areaIncubatorsCountList=new ArrayList<>();
        List<AreaVo> areaVoList = AreaListTool.AREA_VO_LIST;
        for (AreaVo area : areaVoList) {
            AreaVo IncubatorscountVo=new AreaVo();
            QueryWrapper<IncubatorsModel> qw =new QueryWrapper<>();
            qw.eq("area",area.getId());
            Integer count = incubatorsMapper.selectCount(qw);
            IncubatorscountVo.setName(area.getName());
            IncubatorscountVo.setValue(count);
            IncubatorscountVo.setCoord(area.getCoord());
            areaIncubatorsCountList.add(IncubatorscountVo);
        }
        return areaIncubatorsCountList;
    }
}
